package com.example.neusoft.sqlitedemo113cx;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.sqlitedemo.R;

import java.util.List;

/**
 * Created by Neusoft on 2017/5/6.
 */

public  class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.ViewHolder> {
    Context context;
    static Context context2;
    List<ContactBean> list;
    public static ContactDBAdapter contactDBAdapter00;

    MyRVAdapter(Context context, List<ContactBean>list){
        this.context=context;
        setList(list);
    }

    void setList(List<ContactBean> list){
        this.list=list;
    }
    public void clearList(){
        list.clear();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context)
                .inflate(R.layout.item_contact, parent,false);

        final ViewHolder holder=new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ContactBean contactBean=list.get(position);
        holder.name.setText(contactBean.getName());
        holder.tel.setText(contactBean.getTel());
        holder.type.setText(contactBean.getType());
        holder.id.setText(String.valueOf(position+1));
        holder.group.setText(contactBean.getGroup());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContactDBAdapter db=ContactDBAdapter.getInstance(context);
                //contactDBAdapter00.deleteById(Integer.valueOf(holder.id.getText().toString())-1);
                db.deleteById(1);
                list.remove(Integer.valueOf(holder.id.getText().toString())-1);
                notifyDataSetChanged();

            }
        });
        //holder.notifyAll();

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, EditContactActivity.class);
                intent.putExtra("_id", contactBean.get_id());
                intent.putExtra("name", contactBean.getName());
                intent.putExtra("tel", contactBean.getTel());
                intent.putExtra("type", contactBean.getType());
                intent.putExtra("group1", contactBean.getGroup());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list!=null)
            return list.size();
        else
            return 0;
    }

    public  static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name;
        private TextView tel;
        private TextView id;
        private TextView group;
        private TextView type;
        protected Button delete;



        public ViewHolder(View itemView) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.textView_item_contact_name);
            tel=(TextView)itemView.findViewById(R.id.textView_item_contact_tel);
            id=(TextView)itemView.findViewById(R.id.id);
            type=(TextView)itemView.findViewById(R.id.type) ;
            group=(TextView)itemView.findViewById(R.id.group);
            delete=(Button)itemView.findViewById(R.id.delete);


        }
    }

}
